package com.tensquare.user.service.impl;

import com.google.gson.Gson;
import com.tensquare.constants.TokenConstants;
import com.tensquare.entity.JWTInfo;
import com.tensquare.exception.UserTokenException;
import com.tensquare.user.service.AuthService;
import com.tensquare.user.utils.JWTUtil;
import com.tensquare.util.JwtTokenUtil;
import com.tensquare.util.RedisConstant;
import com.tensquare.util.StringHelper;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.TimeUnit;

@Service
public class TokenServiceImpl implements AuthService {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public String refresh(String userId) throws Exception {
        String tokenInfo = redisTemplate.opsForValue().get(RedisConstant.REDIS_USER_TOKEN_KEY + ":" + userId);
        JWTInfo jwtInfo = new Gson().fromJson(tokenInfo, JWTInfo.class);
        return JwtTokenUtil.generateToken(jwtInfo, TokenConstants.SECRET_KEY);
    }

}
